from base.basePage import BasePage
from selenium.webdriver.common.by import By
import time
from page.mail_list_manager import OrgPageElements
from page import BasePageElements
from config import *
import allure

class OrgPage(BasePage):

    def create_org(self, org_name, parent_org):
        """
        创建部门的业务操作封装
        :return:
        """
        # 进入通讯录界面
        with allure.step("进入通讯录界面"):
            self.my_get("{}{}".format(host, OrgPageElements.mail_list_url_path))
        # 点击-+号
        with allure.step("点击-+号"):
            self.my_click(ele=OrgPageElements.add_button)
        # 点击-添加部门
        with allure.step("点击-添加部门"):
            self.my_click(ele=OrgPageElements.add_org_button)
        # 输入部门名称
        with allure.step("输入部门名称：{}".format(org_name)):
            self.my_send_keys(ele=OrgPageElements.name_input, value=org_name)
        # 选择所属部门
        if parent_org:
            with allure.step("选择所属部门：{}".format(parent_org)):
                self.my_click(ele=OrgPageElements.parent_org_box)
                paren_org_choise = (OrgPageElements.parent_org_choise[0], OrgPageElements.parent_org_choise[-1].format(parent_org))
                self.my_click(ele=paren_org_choise)
        # 点击-确定
        time.sleep(1)
        with allure.step("点击-确定"):
            self.my_click(ele=BasePageElements.define_button)

    def update_org(self, old_org_name, new_org_name):
        # 进入通讯录界面
        with allure.step("进入通讯录界面"):
            self.my_get("{}{}".format(host, OrgPageElements.mail_list_url_path))
        # 选择部门：啦啦啦啦，点击设置按钮
        time.sleep(0.5)  # 等待js代码加载
        with allure.step("选择部门：{}".format(old_org_name)):
            org_button = (OrgPageElements.org_button[0], OrgPageElements.org_button[-1].format(old_org_name))
            self.my_click(ele=org_button)
        with allure.step("点击部门功能选择按钮"):
            org_setting_button = (OrgPageElements.org_setting_button[0], OrgPageElements.org_setting_button[-1].format(old_org_name))
            self.my_click(ele=org_setting_button, time_out=20)
        # 点击-修改名称
        with allure.step("点击-修改名称"):
            self.my_click(ele=OrgPageElements.update_org_name_button)
        # 输入部门名称
        with allure.step("输入部门名称：{}".format(new_org_name)):
            self.my_send_keys(ele=OrgPageElements.name_input,
                                  value=new_org_name)
        time.sleep(1)
        # 点击-确定
        with allure.step("点击-确定"):
            self.my_click(ele=BasePageElements.define_button)